import { _decorator, Component, Node, Sprite, Label, Vec3, v3, v2, SpriteFrame, Widget, UITransform, tween, Prefab, instantiate, ParticleSystem, ParticleSystem2D, AudioClip, Color, UIOpacity, randomRangeInt } from 'cc';
import { ColorBlock_GameData } from '../Data/ColorBlock_GameData';
import ColorBlock_GameManager from '../Manager/ColorBlock_GameManager';
import { bdlMgr } from 'db://assets/Script/Manager/BundleManager_S';
import { SSS_Consts } from 'db://assets/Script/Data/SSS_Consts';
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

const { ccclass, property } = _decorator;

@ccclass('ColorBlock_Block')
export default class ColorBlock_Block extends Component {
    @property(Sprite)
    sp: Sprite | null = null;

    col: number = 0;
    row: number = 0;
    id: number = 0;
    isDead: boolean = false;

    //    // 初始化
    init(_id: number, _col: number, _row: number,) {
        this.id = _id;

        this.col = _col;
        this.row = _row;

        this.node.active = true;

        let button = ColorBlock_GameManager.Instance.Buttons[_col];
        let x = button.node.position.x;
        let y = 400;
        this.node.position = v3(x, y, 0);

        // this.node.getComponent(UITransform).width = DragTetris_GameConfig.size;
        // this.node.getComponent(UITransform).height = DragTetris_GameConfig.size;

        this.Refresh();
    }

    async Refresh() {
        this.sp.color = new Color().fromHEX(ColorBlock_GameData.colors[this.id]);
    }

    // GetNodeWorldPostation(_node: Node): Vec3 {
    //     let pos: Vec3 = v3();
    //     if (_node.parent == null) {
    //         pos = v3(_node.getPosition());
    //     } else {
    //         // pos = v3(_node.parent.convertToWorldSpaceAR(_node.getPosition()));
    //         pos = _node.getWorldPosition();
    //     }
    //     return pos;
    // }
    // GetXByX(x_pos: number) {
    //     // let x = (x_pos - (-DragTetris_GameConfig.W / 2 + this.node.getComponent(UITransform).width / 2)) / DragTetris_GameConfig.size + 0.5;
    //     // let x_int = Math.floor(x);
    //     // return x_int;
    // }
    // GetXByY(y_pos: number) {
    //     // let y = (y_pos - (-DragTetris_GameConfig.H / 2 + this.node.getComponent(UITransform).height / 2)) / DragTetris_GameConfig.size + 0.5;
    //     // let x_int = Math.floor(y);
    //     // return x_int;
    // }

    DestroyAnimation(callback: Function = null) {
        const opacity = this.node.getComponent(UIOpacity); // 或者 Sprite、Label 等

        let duration = 0.3;
        tween(this.node)
            .to(duration, { scale: new Vec3(1.7, 1.7, 1.7) })
            .start();

        tween(opacity)
            .to(duration, { opacity: 0 }) // 调整 alpha
            .call(() => {
                this.node.destroy();
            })
            .start();

        // let r = randomRangeInt(1, 3);
        // let root = `audio/水音效${r}`;
        // bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, root, AudioClip).then(res => {
        //     SSS_AudioManager.Instance.playSFX(res);
        // });

        let audio_root = `audio/水音效1`;
        bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
            SSS_AudioManager.Instance.playSFX(res, false);
        });
    }

    Destroy() {
        const index = ColorBlock_GameData.arrBlocks.indexOf(this);
        if (index >= 0) {
            ColorBlock_GameData.arrBlocks.splice(index, 1);
        };

        this.DestroyAnimation();

    }
}